package com.orange.lswidget;

import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.IOException;

import android.content.ContentProvider;
import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
import android.database.sqlite.SQLiteQueryBuilder;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.net.Uri;

public class WallpaperProvider extends ContentProvider {

	// private static final String TAG = "WallpaperProvider";

	public static final String	GALLERY_WALLPAPER_PATH		= "/sdcard/wallpaper.jpg";
	public static final Uri		CONTENT_URI					= Uri.parse("content://com.orange.lockscreen.wallpaper");

	private static final String	WALLPAPER_DATABASE_NAME		= "database";
	private static final String	WALLPAPER_TABLE_NAME		= "database";
	private static final String	WALLPAPER_KEY				= "myWallpaper";
	private static final int	WALLPAPER_DATABASE_VERSION	= 1;

	private DatabaseHelper		mDbHelper;

	@Override
	public int delete(Uri uri, String selection, String[] selectionArgs) {
		return 0;
	}

	@Override
	public String getType(Uri uri) {
		return null;
	}

	@Override
	public Uri insert(Uri uri, ContentValues values) {
		return null;
	}

	@Override
	public boolean onCreate() {
		Context context = getContext();
		mDbHelper = new DatabaseHelper(context);
		return true;
	}

	@Override
	public Cursor query(Uri uri, String[] projection, String selection,
			String[] selectionArgs, String sortOrder) {
		SQLiteQueryBuilder qb = new SQLiteQueryBuilder();
		qb.setTables(WALLPAPER_TABLE_NAME);
		SQLiteDatabase db = mDbHelper.getReadableDatabase();
		return qb.query(db, null, null, null, null, null, null);
	}

	@Override
	public int update(Uri uri, ContentValues values, String selection,
			String[] selectionArgs) {
		int ret = 0;

		byte[] data = getByteFromFile();
		if (data != null) {

			SQLiteDatabase db = mDbHelper.getWritableDatabase();
			db.delete(WALLPAPER_TABLE_NAME, null, null);

			ContentValues wpValues = new ContentValues();
			wpValues.put(WALLPAPER_KEY, data);
			if (-1 == db.insert(WALLPAPER_TABLE_NAME, null, wpValues)) {
				db.close();
				return ret;
			}
			db.close();
			final File file = new File(GALLERY_WALLPAPER_PATH);
			if (file.exists()) {
				try {
					file.delete();
				} catch (Exception e) {
				}
			}
			ret = 1;
		}
		return ret;
	}

	private byte[] getByteFromFile() {
		byte[] data = null;
		Bitmap bitmap = BitmapFactory.decodeFile(GALLERY_WALLPAPER_PATH);
		if (bitmap != null) {
			final ByteArrayOutputStream os = new ByteArrayOutputStream();
			if (bitmap.compress(Bitmap.CompressFormat.PNG, 100, os)) {
				data = os.toByteArray();
				try {
					os.close();
				} catch (IOException e1) {
				}
			}
		}

		return data;
	}

	private static class DatabaseHelper extends SQLiteOpenHelper {

		public DatabaseHelper(Context context) {
			super(context, WALLPAPER_DATABASE_NAME, null,
					WALLPAPER_DATABASE_VERSION);
		}

		@Override
		public void onCreate(SQLiteDatabase db) {
			String sql = "CREATE TABLE " + WALLPAPER_TABLE_NAME
					+ "(_id INTEGER PRIMARY KEY, " + WALLPAPER_KEY + " TEXT);";
			db.execSQL(sql);
		}

		@Override
		public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
			db.execSQL("DROP TABLE IF EXISTS notes");
			onCreate(db);
		}

	}

	// @Override
	// public ParcelFileDescriptor openFile(Uri uri, String mode)
	// throws FileNotFoundException {
	// ParcelFileDescriptor pfd = null;
	// File file = new File(GALLERY_WALLPAPER_PATH);
	// if (file.exists() && file.isFile() && file.canRead()) {
	// pfd = ParcelFileDescriptor.open(file,
	// ParcelFileDescriptor.MODE_READ_ONLY);
	// }
	// return pfd;
	// }
}
